<?php

namespace Home\Controller;

use Think\Controller;
use Think\Page;

/**
 * @todo 			EMBA 众筹
 * @copyright 		睿智从横
 * @author 			guanhanwen
 * @version 		1.0
 * @since 			2014-10-21
 */
class AdminController extends Controller {

    /**
     * @todo	获注册欢迎页
     */
    public function index() {
        $this->isLogin();
        $search = I('get.search');
        if ($search != '') {
            $dream_where['dream_title|dream_content'] = array('like',"%$search%");
        }
        $dream_where['status'] = 0;
        $REQUEST = (array) I('request.');
        $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 1000;
        $list = NULL;
        $dream_count = M('User')->where($dream_where)->count();
        $Page = new \Think\Page($dream_count, $listRows, $REQUEST); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $field = '(select count(*) from dream_support where status=0 and dream_id=dream_user.dream_id) as suport_count,dream_id,user_id,user_name,dream_title,create_date,update_date';
        $dream_list = M('User')->where($dream_where)->field($field)->limit($Page->firstRow . ',' . $Page->listRows)->order('suport_count desc,dream_id desc')->select();
// dump($dream_list);die;
        $user_ids = array();
        foreach ($dream_list as $key => $val) {
            if ((int) $val['user_id'] > 0) {
                $user_ids[] = $val['user_id'];
            }
        }
        $user_ids = implode(',', array_unique($user_ids));
        $user_list = D('Guan')->dreamUserList($user_ids);
        foreach ($dream_list as $key => $val) {
            $dream_list[$key]['school'] = array();
            foreach ($user_list['data'] as $k => $v) {
                if ($val['user_id'] == $v['user_id']) {
                    $dream_list[$key]['school'] = $v['school'];
                    $dream_list[$key]['user_phone'] = $v['phone'];
                }
            }
        }
// dump($user_list['data'][0]);die;
        $this->assign('index_num', $Page->firstRow); // 赋值Key
        $this->assign('_page', $show); // 赋值分页输出
        $this->assign('count', $dream_count);
        $this->assign('_list', $dream_list);
        $this->display('index');
    }

    /**
     * @todo 编辑梦想
     */
    public function editDream() {
        $this->isLogin();
        if ($this->isAjax()) {
            $dream_id = (int) I('post.dream_id');
            $dream_title = (string) I('post.dream_title');
            $dream_content = (string) I('post.dream_content');

            if ($dream_id == 0) {
                $this->ajaxReturn1('', "参数错误", 'LEARN_1020');
            }
            if ($dream_title == '') {
                $this->ajaxReturn1('', "梦想标题不能为空", 'LEARN_1020');
            }
            if ($dream_content == '') {
                $this->ajaxReturn1('', "梦想内容不能为空", 'LEARN_1020');
            }
            $where_dream['dream_id'] = $dream_id;
            $data_dream['dream_title'] = $dream_title;
            $data_dream['dream_content'] = $dream_content;
            $data_dream['update_date'] = NOW_TIME;
            if (false !== M('User')->where($where_dream)->save($data_dream)) {
                $this->ajaxReturn1($dream_id, "操作成功", 'LEARN_1019');
            } else {
                $this->ajaxReturn1('', "操作失败", 'LEARN_1020');
            }
        } else {
            $dream_id = (int) I('get.dream_id');
            if ($dream_id <= 0) {
                $this->error("参数错误");
            }
            $dream_info = M('User')->where(array('dream_id' => $dream_id))->find();
            $this->assign('dream_info', $dream_info);
            $this->display('edit_dream');
        }
    }

    /**
     * @todo 删除梦想
     */
    public function delDream() {
        $dream_id = I('post.dream_id');
        $support_list = M('Support')->where(array('dream_id' => $dream_id))->select();
        $suport_user_ids = array();
        foreach ($support_list as $key => $val) {
            if ((int) $val['suport_user_id'] > 0) {
                $suport_user_ids[] = $val['suport_user_id'];
            }
        }
        $suport_user_ids = array_unique($suport_user_ids);
// 		if(!empty($suport_user_ids)){
// 			M('SupportUser')->where(array('suport_user_id'=>array('in',$suport_user_ids)))->delete();
// 		}
        if (false !== M('Support')->where(array('dream_id' => $dream_id))->delete() && false !== M('User')->where(array('dream_id' => $dream_id))->delete()) {
            $this->ajaxReturn1('', "删除成功", '0');
        } else {
            $this->ajaxReturn1('', "删除失败", '1');
        }
    }

    /**
     * @todo 登录功能
     */
    public function login() {
        if ($this->isAjax()) {
            $account = (string) I('post.account');
            $user_password = (string) I('post.user_password'); //'123456';
            if ($account == '' || $user_password == '') {
                $this->ajaxReturn1('', "账号和密码都不能为空！", 'LEARN_1009');
            }
            import('Common.Org.Util.Encrypt', '', '.php');
            $rule = new \EncryptCopy;
            $where_user['password'] = $rule->encryptByKey($user_password, ENCRYPT_KEY);
            $where_user['account'] = $account;
            $obj_admin = D('Admin');
            $result = $obj_admin->where($where_user)->find();
            //使用用户名、密码和状态的方式进行认证
            if (isset($result['id'])) {
                // dump($result);die;
                session('id', $result['id']);
                session('account', $result['account']);
                $this->ajaxReturn1('', "登录成功", '0');
            } else {
                $this->ajaxReturn1('', "登录账号或密码错误", '1');
            }
        } else {
            session('id', null);
            session('account', null);
            $this->display('login');
        }
    }

    public function isLogin() {
        // dump($_SESSION);die;
        if (session('id') > 0 && session('account') != '' && session('account') != null) {
            return true;
        } else {
            echo "<script>location.href='/Admin/login';</script>";
            die;
        }
    }

    /**
     * @todo 退出登录
     */
    public function loginOut() {
        session(null);
        session_destroy();
        $this->display('Admin:login');
    }

    /**
     * 支持者
     */
    public function support() {
        $this->isLogin();
        $support_user_model = M('SupportUser');
        $map = array(
            'status' => '0',
        );
        $count = $support_user_model->where($map)->count();
        $pageSize = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        $page = new Page($count, $pageSize);
        $field = '(select count(*) from dream_support where status=0 and suport_user_id=dream_support_user.suport_user_id) as support_count,'
                . 'suport_user_id, suport_user_type, open_id, suport_user_name, create_date';
        $list = $support_user_model->where($map)->limit($page->firstRow . ',' . $page->listRows)->field($field)->order('support_count desc, create_date desc')->select();
        foreach ($list as &$value) {
            if ($value['suport_user_type'] == 0) {
                $value['weixin_name'] = $value['suport_user_name'];
                $value['brower_name'] = '无';
            } else {
                $value['weixin_name'] = '无';
                $value['brower_name'] = $value['suport_user_name'];
            }
            if (empty($value['open_id'])) {
                $value['open_id'] = '无';
            }
        }
        $this->assign('index_num', $page->firstRow); // 赋值Key
        $this->assign('_page', $page->show()); // 赋值分页输出
        $this->assign('count', $count);
        $this->assign('_list', $list);
        $this->display();
    }

    /**
     * 执行删除支持者
     */
    public function doDeleteSupport() {
        $id = I('get.id');
        $support_user_model = M('support_user');
        $support_user_model->find($id);
        $support_user_model->status = 1;
        $support_user_model->save();
        $support_model = M('support');
        $map['suport_user_id'] = $id;
        $data['status'] = 1;
        $support_model->where($map)->save($data);
        header('Location: /Admin/support.html');
    }

    /**
     * 导出Excel列表
     */
    public function doExportExcel() {
        $support_user_model = M('support_user');
        $map = array(
            'status' => '0',
        );
        $field = '(select count(*) from dream_support where status=0 and suport_user_id=dream_support_user.suport_user_id) as support_count,'
                . 'suport_user_id, suport_user_type, open_id, suport_user_name, create_date';
        $list = $support_user_model->where($map)->field($field)->order('support_count desc, create_date desc')->select();
        import('Home.Util.PHPExcel.PHPExcel', '', '.php');
        $php_excel = new \PHPExcel();
        //设置标题
        $php_excel->getActiveSheet()->setCellValue('A1', '姓名');
        $php_excel->getActiveSheet()->setCellValue('B1', '微信昵称');
        $php_excel->getActiveSheet()->setCellValue('C1', 'openid');
        $php_excel->getActiveSheet()->setCellValue('D1', '首次参与时间');
        $php_excel->getActiveSheet()->setCellValue('E1', '支持梦想数');
        //输出内容
        $i = 2;
        foreach ($list as $value) {
            $row = $value;
            if ($value['suport_user_type'] == 0) {
                $row['weixin_name'] = $value['suport_user_name'];
                $row['brower_name'] = '无';
            } else {
                $row['weixin_name'] = '无';
                $row['brower_name'] = $value['suport_user_name'];
            }
            if (empty($value['open_id'])) {
                $row['open_id'] = '无';
            }
            $php_excel->getActiveSheet()->setCellValue('A' . $i, $row['brower_name']);
            $php_excel->getActiveSheet()->setCellValue('B' . $i, $row['weixin_name']);
            $php_excel->getActiveSheet()->setCellValue('C' . $i, $row['open_id']);
            $php_excel->getActiveSheet()->setCellValue('D' . $i, date('Y-m-d H:i:s', $row['create_date']));
            $php_excel->getActiveSheet()->setCellValue('E' . $i, $row['support_count']);
            ++$i;
        }
        //导出文件
        $filename = 'EMBA云2015你有啥梦想活动参与者.xlsx';
        $php_excel_writer = new \PHPExcel_Writer_Excel2007($php_excel);
        header('Content-Type: application/download');
        header('Content-Disposition: inline;filename=' . $filename);
        $php_excel_writer->save('php://output');
    }

    /**
     * @todo 数据统计
     */
    public function statistics() {
        //第一部分统计
        $dream_info = $this->statisticsOne();
        $this->statisticsTwo();
        $this->statisticsThree();
        $this->statisticsFour();
        //参与用户数
// dump($dream_list);die;

        $this->assign('dream_info', $dream_info);
        $this->display('statistics');
    }

    /**
     * @todo 获取今天的时间戳
     */
    public function todayTime() {
        return strtotime(date("Y-m-d"));
    }

	//统计一
    public function statisticsOne(){
    	//取今天时间戳
    	$today = $this->todayTime();
    	//取15-1-5时间戳
    	$time_1_5 = strtotime("2015-01-5");//1420387200
    	//获取1-5号之前的发不过梦想的user_id
    	$dream_before_time_1_5 = array();
    	//获取1-5号之前的支持过梦想的user_id
    	$support_before_time_1_5 = array();
// dump($time_1_5);die;
    	$where['status'] = 0;
    	$obj_user = M('User');
    	$dream_list = $obj_user->where($where)->select();
    	//总梦想数
    	$dream_info['dream_count'] = count($dream_list);
    	//新增梦想
    	$dream_app_user = 0;
    	//今日新增梦想数
    	$dream_count_today = 0;
    	//发布梦的用户ID数组
    	$dream_user_ids = array();
    	//来自客户端的用户数
    	$from_app = 0;
    	//今天发布梦想的平台用户
    	$app_user_dream_today = 0;
    	//今日来自客户端的用户数
    	$from_app_today = 0;
    	foreach($dream_list as $key=>$val){
    		if($val['create_date'] > $today){
    			++$dream_count_today;
    			if($val['user_id'] > 0){
    				if($val['from_type'] == 0){
    					++$from_app_today;
    				}
    				++$app_user_dream_today;
    			}
    		}
    		if($val['from_type'] == 0){
    			++$from_app;
    		}
    		if($val['user_id'] > 0){
    			++$dream_app_user;
    		}
    		$dream_user_ids[] = $val['user_id'];
    		//获取一月五号之前发布们想的app用户ID
    		if($val['create_date'] < $time_1_5){
    			$dream_before_time_1_5[] = $val['user_id'];
    		}
    	}
    	$dream_info['dream_count_today'] = $dream_count_today;
    	$dream_info['from_app'] = $from_app;
    	$dream_info['from_app_Percentage'] = sprintf("%.2f", ($from_app / $dream_info['dream_count'])*100);
    	
    	//支持
    	$obj_support = M('Support');
    	$support_list = $obj_support->where($where)->select();
    	$dream_info['support_count'] = count($support_list);
    	//今日新增梦想数
    	$support_count_today = 0;
    	//多少梦想是通过微信发布的
    	$support_from_app = 0;
    	foreach($support_list as $key=>$val){
    		if($val['create_date'] > $today){
    			++$support_count_today;
    		}
    		if($val['from_type'] == 0){
    			++$support_from_app;
    		}
    	}
    	$dream_info['support_count_today'] = $support_count_today;
    	$dream_info['support_from_app'] = $support_from_app;
    	$dream_info['support_from_app_Percentage'] = sprintf("%.2f", ($support_from_app / $dream_info['support_count'])*100);
    	
    	//支持数统计                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    	// 		$where["`dream_user`.suport_count"] = array('gt',10);
    	$field = '(select count(*) from dream_support where status=0 and dream_id=dream_user.dream_id) as suport_count,dream_id,user_id,user_name,dream_title,create_date,update_date';
    	$list = M('User')->where($where)->field($field)->select();
    	$support_10 = 0;
    	$support_20 = 0;
    	$support_50 = 0;
    	$support_100 = 0;
    	foreach($list as $key=>$val){
    		if($val['suport_count'] >10){
    			++$support_10;
    		}
    		if($val['suport_count'] >20){
    			++$support_20;
    		}
    		if($val['suport_count'] >50){
    			++$support_50;
    		}
    		if($val['suport_count'] >100){
    			++$support_100;
    		}
    	}
    	$dream_info['support_10'] = $support_10;
    	$dream_info['support_20'] = $support_20;
    	$dream_info['support_50'] = $support_50;
    	$dream_info['support_100'] = $support_100;
    	
    	
    	//参与人数$dream_user_ids+点赞用户
    	$support_user_list = M('SupportUser')->where(array('status'=>0))->field('suport_user_id,suport_user_type,user_id,open_id,unionid,create_date')->select();
    	//支持梦想的平台用户
    	$support_app_user = 0;
    	//支持梦想的微信用户
    	$support_weixin_user = 0;
		//今天支持梦想的平台用户
		$support_app_user_today = 0;
		//新日新怎支持者
		$support_user_today = 0;
		//来自客户端的支持者
		$from_app_support = 0;
		//今日来自客户端的支持者
		$from_app_support_today = 0;
		//所有今天之前的微信中支持者的open_id
		$weixin_support_list_before = array();
		//所有微信中支持者的open_id
		$weixin_support_list_totle = array();
// 		//支持梦想表中平台用户ID数组
// 		$support_user_ids = array();

		foreach($support_user_list as $key=>$val){
			if(in_array($val['user_id'],$dream_user_ids)){
				unset($support_user_list[$key]);
			}else{
				if($val['create_date'] > $today){
					++$support_user_today;
					if($val['user_id'] > 0){
						++$support_app_user_today;
						if($val['from_type'] == 0){
							++$from_app_support_today;
						}
					}
				}
				if($val['user_id'] > 0){
					++$support_app_user;
				}
				if($val['open_id'] > 0){
					++$support_weixin_user;
				}
				if($val['from_type'] == 0 && $val['user_id'] > 0){
					++$from_app_support;
				}
			}
			if($val['create_date'] < $today && $val['open_id'] != ''){
				$weixin_support_list_before[] = $val['open_id'];
			}
			if($val['open_id'] != ''){
				$weixin_support_list_totle[] = $val['open_id'];
			}
			//获取一月五号之前发布们想的app用户ID
    		if($val['create_date'] < $time_1_5 && $val['user_id'] > 0){
    			$support_before_time_1_5[] = $val['user_id'];
    		}
		}
// dump($support_app_user);die;
		//参与者
		foreach($support_user_list as $key=>$val){
			
		}
		$dream_info['participant_all'] = $participant_all = count($support_user_list) + $dream_info['dream_count'];
		//今日新增参与者
		$dream_info['Participant_all_today'] = $support_user_today + $dream_info['dream_count_today'];
		//社群用户
		$dream_info['app_count'] = $dream_app_user + $support_app_user;
		//来自客户端的参与者
		$dream_info['from_app_count'] = sprintf("%.2f",(($from_app + $from_app_support)/$dream_info['app_count'])*100);
		//今日新增客户端参与用户
		$dream_info['app_count_count_tody'] = $from_app_today + $from_app_support_today;
		//今日新增客户端参与用户
		$dream_info['from_app_percentage_today'] = sprintf("%.2f",($from_app_today+$from_app_support_today)/($support_app_user_today+$app_user_dream_today)*100);
		//社群用户总数
		/*获取一月6号之前的发布活动的用户数组、和支持梦想的平台用户数组，取并集，在后再通过IP表取5号之后的，再取并集*/
		$ip_after_1_5 = M('Ip')->where(array('user_id'=>array('gt',0),'create_date'=>array('gt',$time_1_5)))->field('user_id')->group('user_id')->select();
		$app_user_after_1_5 = array();
		foreach($ip_after_1_5 as $key=>$val){
			$app_user_after_1_5[] = $val['user_id'];
		}
		$before_time_1_5 = array_unique(array_merge($dream_before_time_1_5,$support_before_time_1_5));
		$app_user = count(array_unique(array_merge($app_user_after_1_5,$before_time_1_5)));
		$dream_info['app_totle_count'] = $app_user;
		//社群用户流失 社群用户总数 - 参与的社群用户数
		/*求参与的社群用户数*/
		$dream_info['app_user_loss'] = $dream_info['app_totle_count'] - $dream_info['app_count'];
		//社群用户流失占比 （社群用户总数 - 参与的社群用户数）/社群用户总数
		$dream_info['app_user_loss_percentage'] = sprintf("%.2f",($dream_info['app_totle_count']-$dream_info['participant_all'])/$app_totle_count*100);
		//微信用户总数
		$dream_info['weixin_totle_count'] = count(M('Ip')->where(array('open_id'=>array('neq','')))->group('open_id')->select());
		//微信用户今日新增(取系统内左右微信账号和时间)
		$ipc_list_befort = M('Ip')->where(array('open_id'=>array('neq',''),'create_date'=>array('lt',$today),'from_type'=>1))->field('open_id')->group('open_id')->select();
		$ipc_list_today = M('Ip')->where(array('open_id'=>array('neq',''),'create_date'=>array('gt',$today),'from_type'=>1))->field('open_id')->group('open_id')->select();
		/*从今天范文的记录中取出之前访问过的就是今天新增的*/
		foreach ($ipc_list_today as $key=>$val){
			foreach($ipc_list_befort as $k=>$v){
				if($val['open_id'] == $v['open_id']){
					unset($ipc_list_today[$key]);
				}
			}
			foreach ($weixin_support_list_before as $kc=>$vc){
				if($val['open_id'] == $vc){
					unset($ipc_list_today[$key]);
				}
			}
		}
		$dream_info['weixin_totle_count_today'] = count($ipc_list_today);
		//微信授权流失（微信用户总数-参与活动的微信用户数）
		$weixin_support_list_totle = array_unique($weixin_support_list_totle);
		$dream_info['weixin_user_loss'] = $dream_info['weixin_totle_count']-count($weixin_support_list_totle);
		//微信流失占比
		$dream_info['weixin_user_loss_percentage'] = sprintf("%.2f",($dream_info['weixin_user_loss']/$dream_info['weixin_totle_count'])*100);
// dump($dream_info['weixin_user_loss_percentage']);die;
    	return $dream_info;
    }

    //统计二
    public function statisticsTwo() {
        $ip_model = M('ip');
        //服务访问总次数
        $count = $ip_model->count();
        $this->assign('st_count', $count);
        //服务访问总次数新增
        $map['create_date'] = array('gt', strtotime(date('Y-m-d')));
        $today_count = $ip_model->where($map)->count();
        $this->assign('st_today_count', $today_count);
        //社区用户分别占比
        $map = array(
            'user_id' => array('neq', 0),
        );
        $user_count = $ip_model->where($map)->count();
        $this->assign('st_count_user_per', number_format($user_count / $count * 100, 2));
        //社区用户分别占比今日
        $map = array(
            'user_id' => array('neq', 0),
            'create_date' => array('gt', strtotime(date('Y-m-d'))),
        );
        $today_user_count = $ip_model->where($map)->count();
        $this->assign('st_today_count_user_per', number_format($today_user_count / $today_count * 100, 2));
        //客户端占比
        $map = array(
            'user_id' => array('neq', 0),
            'from_type' => 0,
        );
        $client_count = $ip_model->where($map)->count();
        $this->assign('st_count_client_per', number_format($client_count / $count * 100, 2));
        //客户端占比今日
        $map = array(
            'user_id' => array('neq', 0),
            'from_type' => 0,
            'create_date' => array('gt', strtotime(date('Y-m-d'))),
        );
        $today_client_count = $ip_model->where($map)->count();
        $this->assign('st_today_count_client_per', number_format($today_client_count / $today_count * 100, 2));
    }

    //统计三
    public function statisticsThree() {
        $ip_model = M('ip');
        //服务访问总次数
        $count = $ip_model->count('distinct ip');
        $this->assign('ss_count', $count);
        //服务访问总次数新增
        $map['create_date'] = array('gt', strtotime(date('Y-m-d')));
        $today_count = $ip_model->where($map)->count('distinct ip');
        $this->assign('ss_today_count', $today_count);
        //社区用户分别占比
        $map = array(
            'user_id' => array('neq', 0),
        );
        $user_count = $ip_model->where($map)->count('distinct ip');
        $this->assign('ss_count_user_per', number_format($user_count / $count * 100, 2));
        //社区用户分别占比今日
        $map = array(
            'user_id' => array('neq', 0),
            'create_date' => array('gt', strtotime(date('Y-m-d'))),
        );
        $today_user_count = $ip_model->where($map)->count('distinct ip');
        $this->assign('ss_today_count_user_per', number_format($today_user_count / $today_count * 100, 2));
        //客户端占比
        $map = array(
            'user_id' => array('neq', 0),
            'from_type' => 0,
        );
        $client_count = $ip_model->where($map)->count('distinct ip');
        $this->assign('ss_count_client_per', number_format($client_count / $count * 100, 2));
        //客户端占比今日
        $map = array(
            'user_id' => array('neq', 0),
            'from_type' => 0,
            'create_date' => array('gt', strtotime(date('Y-m-d'))),
        );
        $today_client_count = $ip_model->where($map)->count('distinct ip');
        $this->assign('ss_today_count_client_per', number_format($today_client_count / $today_count * 100, 2));
    }

    //统计四
    public function statisticsFour() {
        $ip_model = M('ip');
        //服务访问总次数
        $map['is_top'] = 1;
        $count = $ip_model->where($map)->count();
        $this->assign('sf_count', $count);
        //服务访问总次数新增
        $map['create_date'] = array('gt', strtotime(date('Y-m-d')));
        $map['is_top'] = 1;
        $today_count = $ip_model->where($map)->count();
        $this->assign('sf_today_count', $today_count);
        //社区用户分别占比
        $map = array(
            'user_id' => array('neq', 0),
            'is_top' => 1,
        );
        $user_count = $ip_model->where($map)->count();
        $this->assign('sf_count_user_per', number_format($user_count / $count * 100, 2));
        //社区用户分别占比今日
        $map = array(
            'user_id' => array('neq', 0),
            'create_date' => array('gt', strtotime(date('Y-m-d'))),
            'is_top' => 1,
        );
        $today_user_count = $ip_model->where($map)->count();
        $this->assign('sf_today_count_user_per', number_format($today_user_count / $today_count * 100, 2));
        //客户端占比
        $map = array(
            'user_id' => array('neq', 0),
            'from_type' => 0,
            'is_top' => 1,
        );
        $client_count = $ip_model->where($map)->count();
        $this->assign('sf_count_client_per', number_format($client_count / $count * 100, 2));
        //客户端占比今日
        $map = array(
            'user_id' => array('neq', 0),
            'from_type' => 0,
            'create_date' => array('gt', strtotime(date('Y-m-d'))),
            'is_top' => 1,
        );
        $today_client_count = $ip_model->where($map)->count();
        $this->assign('sf_today_count_client_per', number_format($today_client_count / $today_count * 100, 2));
    }

    
    
    
    /*
     * @todo excel导出梦想列表
    */
    public function outPutDream() {
    	$this->isLogin();
    	//延长执行时间，知道执行完成为止
    	set_time_limit(0);
    	// 准备数据源
        $search = I('get.search');
        if ($search != '') {
            $dream_where['dream_title'] = $search;
        }
        $dream_where['status'] = 0;
        $REQUEST = (array) I('request.');
        $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        $list = NULL;
        $dream_count = M('User')->where($dream_where)->count();
        $field = '(select count(*) from dream_support where status=0 and dream_id=dream_user.dream_id) as suport_count,dream_id,user_id,user_name,dream_title,dream_content,create_date,update_date';
        $dream_list = M('User')->where($dream_where)->field($field)->order('suport_count desc,dream_id desc')->select();
        $user_ids = array();
        foreach ($dream_list as $key => $val) {
            if ((int) $val['user_id'] > 0) {
                $user_ids[] = $val['user_id'];
            }
        }
        $user_ids = implode(',', array_unique($user_ids));
        $user_list = D('Guan')->dreamUserList($user_ids);
        foreach ($dream_list as $key => $val) {
            $dream_list[$key]['school'] = '';
            foreach ($user_list['data'] as $k => $v) {
                if ($val['user_id'] == $v['user_id']) {
                	if(!empty($v['school'])){
                		$school = array();
                		foreach($v['school'] as $ks=>$vs){
                			$school[] = $vs['school_name'].'|'.$vs['grade_name'];
                		}
                	}
                    $dream_list[$key]['school'] = implode(',',$school);
                    $dream_list[$key]['user_phone'] = $v['phone'];
                }
            }
        }
// iconv("utf8mb4", "UTF-8",$dream_list[$i]['dream_content']);
// dump(iconv("UTF-8", "GB2312//IGNORE",$dream_list[1]['dream_content']));die;
    	$dream_list = array_merge($dream_list);
    	import("Common.Org.PHPExcel.PHPExcel",'','.php');
    	import("Common.Org.PHPExcel.PHPExcel.Writer.Excel5",'','.php');
    	import("Common.Org.PHPExcel.PHPExcel.Writer.Excel2007",'','.php');
    	import("Common.Org.PHPExcel.PHPExcel.IOFactory",'','.php');
    	$objPHPExcel = new \PHPExcel();
    
    	$first_count = count($dream_list);
    	// dump($first_count);die;
    	$objPHPExcel->getActiveSheet ( 0 )->getDefaultColumnDimension ()->setWidth ( 30 ); // 設置單元格寬度
    	$objPHPExcel->getActiveSheet ( 0 )->setTitle ( '共('.$first_count. ')条' ); // 設置當前工作表的名稱
    
    	/**
    	 * 设置标题
    	*/
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 0, 1, '序号' );
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 1, 1, '标题' );
//     	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 1, 1, '内容' );
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 2, 1, '发布者' );
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 3, 1, '支持数' );
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 4, 1, '手机' );
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 5, 1, '学校信息' );
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 6, 1, '发布时间' );
    
    	for($i = 0; $i <= 6; $i ++){
    		$objPHPExcel->getActiveSheet ( 0 )->getStyleByColumnAndRow ( $i, 1 )->getFont ()->setBold ( true );//设置字体加粗
    		for($j = 0;$j<=$first_count+1;$j++){
    			$objPHPExcel->getActiveSheet ( 0 )->getStyleByColumnAndRow ( $i, $j )->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//文字居中
    		}
    	}
    
    	$j = 2;
    	for($i = 0; $i < count($dream_list); $i ++)
    	{
    
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 0, $j, $j-1 );
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 1, $j, $dream_list[$i]['dream_title']);
//     	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 1, $j, iconv("UTF-8", "GB2312//IGNORE",$dream_list[1]['dream_content']));
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 2, $j, $dream_list[$i]['user_name']);
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 3, $j, $dream_list[$i]['suport_count']);
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 4, $j, $dream_list[$i]['user_phone']);
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 5, $j, $dream_list[$i]['school']);
    	$objPHPExcel->getActiveSheet ( 0 )->setCellValueByColumnAndRow ( 6, $j, date('Y-m-d H:i:s',$dream_list[$i]['create_date']));

    
		$j ++;
    	}
    
    
    
    	/**
    	* 下载生成后的excel
    	*/
    	$objPHPExcel->setActiveSheetIndex ( 0 ); // 设置打开后默认显示的工作空间
    	$excelName = "EMBA云2015梦想活动统计表-".date('Ymd').'.xls';
    	// 			@$excelName = rand(1000, 9999).date('Ymd').'.xls';
    	$objWriter = \PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel5' );
    	header ( "Content-Type: application/force-download" );
    	header ( "Content-Type: application/octet-stream" );
		header ( "Content-Type: application/download" );
			// @header("Content-Disposition: attachment; filename=".urlencode($excelName));
    	header ( "Content-Disposition: attachment; filename=" . $excelName );
    	header ( "Content-Transfer-Encoding: binary" );
    			header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
    	header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
    	header ( "Pragma: no-cache" );
    	$objWriter->save ( 'php://output' );
    }
}
